//
//  Cell.swift
//  clreader
//
//  Created by colin on 2019/3/14.
//  Copyright © 2019 colin. All rights reserved.
//

extension EBookDetailViewController {
    
    class CellHeader: UITableViewCell {
        static let reuseId = "CellHeader"
        var cellHeight:CGFloat = 0
        
        private lazy var imageViewPhoto = UIImageView()
        private lazy var labelName = UILabel()
        private lazy var labelAuthor = UILabel()
        private lazy var labelPublishTime = UILabel()
        private lazy var labelRecommand = UILabel()
        
        func refresh(data:JSONObject?) {
            // 封面
            if let cover_url = data?["cover_url"] as? String,!cover_url.isEmpty,
                let baseUrl = URL(string: Router.baseUrl) {
                let imageUrl = baseUrl.appendingPathComponent(cover_url)
                self.imageViewPhoto.kf.setImage(with: imageUrl)
            } else {
                self.imageViewPhoto.image = nil
            }
            
            self.labelName.text = data?["title"] as? String
            self.labelAuthor.text = data?["author"] as? String
            self.labelPublishTime.text = data?["publish_time"] as? String
            
            if let intro = data?["content"] as? String {
                self.labelRecommand.attributedText = intro.htmlAttributedString
            } else {
                self.labelRecommand.text = ""
            }

            
            self.cellHeight = 30.0
            
            // 图片高度
            self.cellHeight += 110.0
            
            // 推荐语
            self.cellHeight += 20.0
            
            self.cellHeight += 15.0
            self.cellHeight += UIFont.systemFont(ofSize: 17.0, weight: .bold).lineHeight
            
            self.cellHeight += 15.0
            let content = data?["content"] as? String
            let containerWidth = UIScreen.main.bounds.width - 15.0 - 15.0 - 25.0 - 25.0
            if let containerHeight = content?.htmlAttributedString?.boundingRect(with: CGSize(width: containerWidth, height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil).size.height {
                self.cellHeight += containerHeight
            }
            self.cellHeight += 15.0
        }
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.selectionStyle = .none
            self.backgroundColor = UIColor.clear
            self.contentView.backgroundColor = UIColor.clear
            
            // 图书信息
            self.contentView.do {
                // 封面
                self.imageViewPhoto.do {
                    $0.layer.masksToBounds = true
                    $0.layer.cornerRadius = 3.0
                }
                $0.addSubview(self.imageViewPhoto)
                self.imageViewPhoto.snp.makeConstraints({ (make) in
                    make.size.equalTo(CGSize(width: 80.0, height: 110.0))
                    make.top.equalToSuperview().offset(30.0)
                    make.left.equalToSuperview().offset(15.0)
                })
                
                // 阴影背景
                let imageViewInfoShadow = UIView().then {
                    $0.backgroundColor = UIColor.black
                    $0.layer.cornerRadius = 3.0
                    $0.layer.shadowColor = UIColor.black.cgColor
                    $0.layer.shadowOffset = CGSize(width:2.0,height:4.0)
                    $0.layer.shadowOpacity = 0.3 //阴影
                }
                $0.insertSubview(imageViewInfoShadow, belowSubview: self.imageViewPhoto)
                imageViewInfoShadow.snp.makeConstraints({ (make) in
                    make.edges.equalTo(self.imageViewPhoto)
                })
                
                // 名称
                self.labelName.do {
                    $0.textColor = UIColor(hex: "343434")
                    $0.font = UIFont.systemFont(ofSize: 19.0, weight: .bold)
                }
                $0.addSubview(self.labelName)
                self.labelName.snp.makeConstraints({ (make) in
                    make.left.equalTo(self.imageViewPhoto.snp.right).offset(15.0)
                    make.top.equalTo(self.imageViewPhoto.snp.top).offset(5.0)
                    make.right.equalToSuperview().offset(-15.0)
                })
                
                // 作者
                let labelAuthorTag = UILabel().then {
                    $0.layer.cornerRadius = 2.0
                    $0.layer.masksToBounds = true
                    $0.textColor = UIColor(hex: "848484")
                    $0.font = UIFont.systemFont(ofSize: 10.0, weight: .light)
                    $0.text = "作者"
                    $0.textAlignment = .center
                    $0.backgroundColor = UIColor(hex: "EFEFEF")
                }
                $0.addSubview(labelAuthorTag)
                labelAuthorTag.snp.makeConstraints({ (make) in
                    make.left.equalTo(self.imageViewPhoto.snp.right).offset(15.0)
                    make.top.equalTo(self.labelName.snp.bottom).offset(15.0)
                    make.size.equalTo(CGSize(width: 36.0, height: 17.0))
                })
                self.labelAuthor.do {
                    $0.textColor = UIColor(hex: "343434")
                    $0.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
                }
                $0.addSubview(self.labelAuthor)
                self.labelAuthor.snp.makeConstraints({ (make) in
                    make.left.equalTo(labelAuthorTag.snp.right).offset(15.0)
                    make.right.equalToSuperview().offset(-15.0)
                    make.centerY.equalTo(labelAuthorTag.snp.centerY)
                })
                
                // 出版时间
                self.labelPublishTime.do {
                    $0.textColor = UIColor(hex: "848484")
                    $0.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
                }
                $0.addSubview(self.labelPublishTime)
                self.labelPublishTime.snp.makeConstraints({ (make) in
                    make.left.equalTo(self.imageViewPhoto.snp.right).offset(15.0)
                    make.bottom.equalTo(self.imageViewPhoto.snp.bottom).offset(-3.0)
                    make.right.equalToSuperview().offset(-15.0)
                })
                
                // 推荐语
                let recommandContainer = UIView().then {
                    $0.layer.cornerRadius = 5.0
                    $0.layer.masksToBounds = true
                    
                    let imageViewBg = UIImageView(image: UIImage(named: "recommand_bg"))
                    $0.addSubview(imageViewBg)
                    imageViewBg.snp.makeConstraints({ (make) in
                        make.edges.equalToSuperview()
                    })
                    
                    let labelRecommandTag = UILabel().then {
                        $0.textColor = UIColor(hex: "343434")
                        $0.font = UIFont.systemFont(ofSize: 17.0, weight: .bold)
                        $0.text = "推荐语"
                    }
                    $0.addSubview(labelRecommandTag)
                    labelRecommandTag.snp.makeConstraints({ (make) in
                        make.left.equalToSuperview().offset(25.0)
                        make.top.equalToSuperview().offset(15.0)
                    })
                    
                    self.labelRecommand.do {
                        $0.textColor = UIColor(hex: "343434")
                        $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
                        $0.numberOfLines = 0
                    }
                    $0.addSubview(self.labelRecommand)
                    self.labelRecommand.snp.makeConstraints({ (make) in
                        make.left.equalToSuperview().offset(25.0)
                        make.right.equalToSuperview().offset(-25.0)
                        make.top.equalTo(labelRecommandTag.snp.bottom).offset(15.0)
                        make.bottom.equalToSuperview().offset(-15.0)
                    })
                    
                }
                $0.addSubview(recommandContainer)
                recommandContainer.snp.makeConstraints({ (make) in
                    make.left.equalToSuperview().offset(15.0)
                    make.right.equalToSuperview().offset(-15.0)
                    make.top.equalTo(self.imageViewPhoto.snp.bottom).offset(20.0)
                    make.bottom.equalToSuperview()
                })
                
                // 阴影背景
                let recommandContainerShadow = UIView().then {
                    $0.backgroundColor = UIColor.white
                    $0.layer.cornerRadius = 3.0
                    $0.layer.shadowColor = UIColor.black.cgColor
                    $0.layer.shadowOffset = CGSize(width:2.0,height:4.0)
                    $0.layer.shadowOpacity = 0.3 //阴影
                }
                $0.insertSubview(recommandContainerShadow, belowSubview: recommandContainer)
                recommandContainerShadow.snp.makeConstraints({ (make) in
                    make.edges.equalTo(recommandContainer)
                })
                
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    class CellIntro: UITableViewCell {
        
        static let reuseId = "CellIntro"
        var cellHeight:CGFloat = 0
        
        private lazy var contentWebView = CLEmbededWebView(frame: CGRect.zero)
        private var heightChangedHandle:((_ height_:CGFloat)->Void)?
        
        func refresh(data:JSONObject?,heightChangedHandle:((_ height_:CGFloat)->Void)?) {
            self.heightChangedHandle = heightChangedHandle
            self.contentWebView.content = data?["intro"] as? String
        }
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.selectionStyle = .none
            self.backgroundColor = UIColor.clear
            self.contentView.backgroundColor = UIColor.clear
            
            self.contentWebView.webViewHeightChanged = { [weak self] (height_) in
                self?.cellHeight = height_
                self?.heightChangedHandle?(height_)
            }
            self.contentView.addSubview(self.contentWebView)
            self.contentWebView.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(15.0)
                make.right.equalToSuperview().offset(-15.0)
                make.top.bottom.equalToSuperview()
            }
            
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    class CellCopyright: UITableViewCell {
        
        static let reuseId = "CellCopyright"
        var cellHeight:CGFloat {
            return 90.0
        }
        
        private lazy var labelName = UILabel()
        private lazy var labelAuthor = UILabel()
        private lazy var labelPress = UILabel()
        private lazy var labelTime = UILabel()
        
        func refresh(data:JSONObject?) {
            self.labelTime.text = data?["title"] as? String
            self.labelAuthor.text = data?["author"] as? String
            
            let goodsBook = data?["goodsBook"] as? JSONObject
            self.labelPress.text = goodsBook?["publish"] as? String
            self.labelTime.text = goodsBook?["publish_date"] as? String
        }
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.selectionStyle = .none
            self.backgroundColor = UIColor.clear
            self.contentView.backgroundColor = UIColor.clear
            
            // 名称
            let labelNameTag = UILabel().then {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
                $0.text = "书名"
            }
            self.contentView.addSubview(labelNameTag)
            labelNameTag.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15.0)
                make.top.equalToSuperview()
                make.width.equalTo(70.0)
            })
            
            self.labelName.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
            }
            self.contentView.addSubview(self.labelName)
            self.labelName.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(100.0)
                make.centerY.equalTo(labelNameTag.snp.centerY)
                make.right.equalToSuperview().offset(-15.0)
            })
            
            // 作者
            let labelAuthorTag = UILabel().then {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
                $0.text = "作者"
            }
            self.contentView.addSubview(labelAuthorTag)
            labelAuthorTag.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15.0)
                make.top.equalTo(labelNameTag.snp.bottom).offset(5.0)
                make.width.equalTo(70.0)
            })
            
            self.labelAuthor.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
            }
            self.contentView.addSubview(self.labelAuthor)
            self.labelAuthor.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(100.0)
                make.centerY.equalTo(labelAuthorTag.snp.centerY)
                make.right.equalToSuperview().offset(-15.0)
            })
            
            // 出版社
            let labelPressTag = UILabel().then {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
                $0.text = "出版社"
            }
            self.contentView.addSubview(labelPressTag)
            labelPressTag.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15.0)
                make.top.equalTo(labelAuthorTag.snp.bottom).offset(5.0)
                make.width.equalTo(70.0)
            })
            
            self.labelPress.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
            }
            self.contentView.addSubview(self.labelPress)
            self.labelPress.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(100.0)
                make.centerY.equalTo(labelPressTag.snp.centerY)
                make.right.equalToSuperview().offset(-15.0)
            })
            
            // 出版时间
            let labelTimeTag = UILabel().then {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
                $0.text = "出版时间"
            }
            self.contentView.addSubview(labelTimeTag)
            labelTimeTag.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15.0)
                make.top.equalTo(labelPressTag.snp.bottom).offset(5.0)
                make.width.equalTo(70.0)
            })
            
            self.labelTime.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .light)
            }
            self.contentView.addSubview(self.labelTime)
            self.labelTime.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(100.0)
                make.centerY.equalTo(labelTimeTag.snp.centerY)
                make.right.equalToSuperview().offset(-15.0)
            })
            
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    class CellExpand: UITableViewCell {
        
        static let reuseId = "CellExpand"
        var cellHeight:CGFloat {
            return 70.0
        }
        
        private lazy var imageViewInfo = UIImageView()
        private lazy var labelName = UILabel()
        private lazy var labelAuthor = UILabel()
        
        func refresh(data:JSONObject?) {
            self.labelName.text = data?["title"] as? String
            self.labelAuthor.text = data?["title"] as? String
            
            // 图片
            if let cover_url = data?["cover_url"] as? String,!cover_url.isEmpty,
                let baseUrl = URL(string: Router.baseUrl) {
                let imageUrl = baseUrl.appendingPathComponent(cover_url)
                self.imageViewInfo.kf.setImage(with: imageUrl)
            } else {
                self.imageViewInfo.image = nil
            }
        }
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.backgroundColor = UIColor.clear
            self.contentView.backgroundColor = UIColor.clear
            
            self.imageViewInfo.do {
                $0.layer.cornerRadius = 3.0
                $0.layer.masksToBounds = true
            }
            self.contentView.addSubview(self.imageViewInfo)
            self.imageViewInfo.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-15.0)
                make.centerY.equalToSuperview()
                make.size.equalTo(CGSize(width: 70.0, height: 55.0))
            }
            
            self.labelName.do {
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 15.0, weight: .bold)
            }
            self.contentView.addSubview(self.labelName)
            self.labelName.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15.0)
                make.top.equalTo(self.imageViewInfo).offset(10.0)
                make.right.equalTo(self.imageViewInfo.snp.left).offset(-15.0)
            })
            
            self.labelAuthor.do {
                $0.textColor = UIColor(hex: "828282")
                $0.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
            }
            self.contentView.addSubview(self.labelAuthor)
            self.labelAuthor.snp.makeConstraints({ (make) in
                make.left.equalToSuperview().offset(15.0)
                make.bottom.equalTo(self.imageViewInfo.snp.bottom).offset(-10.0)
                make.right.equalToSuperview().offset(-15.0)
            })
            
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
}
